package com.htht.smartschool.util.redis;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.htht.smartschool.util.system.ConfigUtil;
import com.udpwork.ssdb.Response;
import com.udpwork.ssdb.SSDB;

public class SSdbUtil {  
	
	private SSdbUtil() {}  
	public static SSDB ssdb;
	/**
     * 构建redis连接池
     * 
     * @param ip
     * @param port
     * @return JedisPool
     */
	public static void setConfigRedis(String host,Integer port){
		ssdb = null;
		try {
			ssdb = new SSDB(host, port, 10000);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	 /**
     * 返还到连接池
     * 
     * @param pool 
     * @param ssdb
     */
    public static void returnResource(SSDB ssdb) {
    	ssdb.close();
        ssdb = null;
    }
    /**
     * 获取数据
     * @param key
     * @return
     */
	public static List<byte[]> get(String key,Long min,Long max){
		Response resp = null;
        try {
        	setConfigRedis(ConfigUtil.getProperty("ssdb","host"),Integer.parseInt(ConfigUtil.getProperty("ssdb","port")));
        	resp = ssdb.zscan(key, "", min, max,9999999);
        } catch (Exception e) {
        	//返还到连接池
            returnResource(ssdb);
            e.printStackTrace();
        } finally {
        	//返还到连接池
            returnResource(ssdb);
        }
        return resp.keys;
	}
	
	/**
	 * 添加数据
	 * @param key
	 * @param value
	 */
	public static Long add(String key, Long score, byte[] member){
		Long status = null;
        try {
        	setConfigRedis(ConfigUtil.getProperty("ssdb","host"),Integer.parseInt(ConfigUtil.getProperty("ssdb","port")));
    		ssdb.zset(key, member, score);
        } catch (Exception e) {
        	 //返还到连接池
            returnResource(ssdb);
            e.printStackTrace();
        } finally {
        	//返还到连接池
            returnResource(ssdb);
        }
        return status;
	}
	public static Long add(String key, Long score, String member){
		Long status = null;
        try {
        	setConfigRedis(ConfigUtil.getProperty("ssdb","host"),Integer.parseInt(ConfigUtil.getProperty("ssdb","port")));
    		ssdb.zset(key,member,score);
        } catch (Exception e) {
        	 //返还到连接池
            returnResource(ssdb);
            e.printStackTrace();
        } finally {
        	//返还到连接池
            returnResource(ssdb);
        }
        return status;
	}
}  
